package rs.dalao.syc.passport.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:SendSmsUtil
 * @Description: 阿里云短信工具类
 * @author: TracyYang
 * @date:2019年8月28日 上午10:15:40
 */
@Slf4j
public class SendSmsUtil {

    // 签名
    private static final String signName = "四叶草";

    // 短信模板
    private static final String templateCode = "SMS_462570363";

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5t68jizRYCYVWHgss771";
    private static final String accessKeySecret = "NenwKwFn5PJDKeynAh8D4AKwpP6qXk";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String PRODUCT = "Dysmsapi";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
    //验证码
    private static int code;

    /**
     * 发送短信通知
     *
     * @param mobile 手机号
     * @return 执行结果
     */
    public static Integer sendSMS(String mobile) {
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);

            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();

            request.setMethod(MethodType.POST);

            // 手机号可以单个也可以多个（多个用逗号隔开，如：15*******13,13*******27,17*******56）
            request.setPhoneNumbers(mobile);

            request.setSignName(signName);

            request.setTemplateCode(templateCode);
            //随机生成6位验证码
            code = (int) ((Math.random() * 9 + 1) * 100000);

            // 可选:您的验证码为 ${code} ，该验证码5分钟内有效，请勿泄露于他人。,此处的值为
            // 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
            request.setTemplateParam("{code:"+code+"}");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
                log.info("发送成功,code:" + sendSmsResponse.getCode());
                return code;
            } else {
                log.info("发送失败,code:" + sendSmsResponse.getCode());
                return -1;
            }
        } catch (ClientException e) {
            log.error("发送失败,系统错误！", e);
            return -1;
        }
    }
    public static String getSplitString(List<String> str) {
        StringBuilder newS = new StringBuilder();
        if (str != null && str.size() > 0) {
            for (String s : str) {
                newS.append(s).append(",");
            }
        }
        if (newS.length() > 0)
            newS.deleteCharAt(newS.length() - 1);// 删除最后一个多余的逗号
        return newS.toString();
    }

    public static void main(String[] args) {
//        多发
        /*List<String> mobiles = new ArrayList<>();
        mobiles.add("15*******13");
        mobiles.add("13*******27");
        mobiles.add("17*******56");
        String s= getSplitString(mobiles);
        System.out.println(SendSmsUtil.sendSMS(s));*/
//         单发
        System.out.println(SendSmsUtil.sendSMS("18883783257"));
    }
}

